package com.atguigu.gulimall.coupon.service.impl;

import com.atguigu.gulimall.coupon.entity.SeckillSkuRelationEntity;
import com.atguigu.gulimall.coupon.service.SeckillSkuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.coupon.dao.SeckillSessionDao;
import com.atguigu.gulimall.coupon.entity.SeckillSessionEntity;
import com.atguigu.gulimall.coupon.service.SeckillSessionService;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询：最近3天的秒杀活动
     */
    @Override
    public List<SeckillSessionEntity> getLates3DaySession() {
        //计算最近3天的时间
        String start = get3DaysTimeStart();
        String end = get3DaysTimeEnd();
        //1、查询最近3天的秒杀活动
        List<SeckillSessionEntity> list = this.list(new QueryWrapper<SeckillSessionEntity>().between("start_time", start, end));
        //2、查询最近3天活动涉及到的所有秒杀商品
        if (list != null && list.size() > 0) {
            List<SeckillSessionEntity> collect = list.stream().map(item -> {
                Long id = item.getId();
                List<SeckillSkuRelationEntity> relationEntities = seckillSkuRelationService.list(new QueryWrapper<SeckillSkuRelationEntity>().eq("promotion_session_id", id));
                item.setRelationSkus(relationEntities);
                return item;
            }).collect(Collectors.toList());
            return collect;
        } else {
            return null;
        }
    }

    /**
     * 最近3天的起始时间（今天 00:00:00 ）
     * jdk8的时间api
     */
    private String get3DaysTimeStart() {
        //计算今天的时间（使用jdk8的 LocalDate等api）
        LocalDate now = LocalDate.now();//今天的日期
        LocalTime min = LocalTime.MIN;//最小时间 00:00:00
        LocalDateTime start = LocalDateTime.of(now, min);//日期+时间=今天日期 00:00:00
        String format = start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }

    /**
     * 结最近3天的束时间（今天+2 23:59:59 ）
     * jdk8的时间api
     */
    private String get3DaysTimeEnd() {
        //计算后3天的时间（使用jdk8的 LocalDate等api）
        LocalDate now = LocalDate.now();//今天的日期
        LocalDate end = now.plusDays(2);//今天的日期+2
        LocalTime max = LocalTime.MAX;//最大时间 23:59:59
        LocalDateTime end1 = LocalDateTime.of(end, max);//日期+时间=今天日期+2 23:59:59
        String format = end1.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }


}